/*
 COPYRIGHT (C) 1984,1985,1986,1988,1989 Apple Computer,Inc.
 All rights reserved
*/

#include	<types.h>
#include	<memory.h>
#include	<menus.h>
#include	<resources.h>
#include	<lists.h>
#include	<ToolUtils.h>

#include	"ResEd.h"

#define IconSize 128
#define cellInset 8

/* This routine simply looks up in the list at the given cell, extracts the ID and
	gets the resource called for.  Returns NIL if not found.  Note, this assumes
	the resfile is set up correctly */
Handle IconFetch (Cell lCell, ListHandle lHandle)
{
	short		id;
	short 	len = 2;
	Handle	tempH;
		
	LGetCell((Ptr)&id, &len, lCell, lHandle);			/* Get the ID from the list.	*/
	if (len == 2) {																/* ID must be 2 bytes.				*/
		/* Load the resource since we want to draw it. 														*/
		tempH = Get1Res((ResType)'ICON', id );
		
		if (SizeResource(tempH) >= IconSize)
			return tempH;
		}
	return NULL;
}

/* This is the custom drawProc for the list (which contains the resource ID's).  It
	simply draws the icon in the given rect and frames a selection rect around it (if
	necessary). */
pascal void  DrawCell(short message, Boolean lSelect, Rect *lRect, Point lCell,
                   		short lDataOffset, short lDataLen, ListHandle lHandle)
{
	Handle tempH;
	
	#pragma	unused (lDataOffset, lDataLen)	/* Not needed here.								*/

	if (message == lInitMsg) {
			(*lHandle)->indent.h = 8;
			(*lHandle)->indent.v = 8;
			}

	if ((message == lDrawMsg) || (message == lHiliteMsg)) {
		/* Always use the right sized rectangle. */
		lRect->bottom = lRect->top + (*lHandle)->cellSize.v;
		lRect->right = lRect->left + (*lHandle)->cellSize.h;
		
		tempH = IconFetch(lCell, lHandle);
		EraseRect(lRect);
		InsetRect(lRect, (*lHandle)->indent.h, (*lHandle)->indent.v);
		
		if (lSelect == true) {
			FrameRect(lRect);								/* Select the icon by framing it. */
			}
			
		if (tempH != NULL) {
			if (*tempH != NULL) {
				InsetRect(lRect, (*lHandle)->indent.h, (*lHandle)->indent.v);
				PlotIcon(lRect, tempH);
				}
			}
		}
}
